﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;

namespace BAL
{
    public partial class Login : Form
    {
        private LoginService.BarcodeJudgeServices bjs = new BAL.LoginService.BarcodeJudgeServices();

        public Login()
        {
            InitializeComponent();
            string location = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase.ToString());
            string path = location + "\\Machines.txt";
            StreamReader sr = new StreamReader(path,Encoding.UTF8);
            string str = sr.ReadToEnd();
            tbName.Text = str;
            sr.Close();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string username = tbName.Text.ToString().Trim();
            string password = tbPassword.Text.ToString().Trim();
            string userid = "";

            if (username == "" || username == null) 
            {
                username = "admin";
                //MessageBox.Show("用户名不能为空！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                //return; 
            }
            if (password == "" || password == null) 
            {
                password = "123456";
                //MessageBox.Show("密码不能为空！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                //return; 
            }

            string ls_return = bjs.login(username, password, out userid).Trim();

            if (ls_return == "1")
            {
                //读取用户
                string location1 = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase.ToString());
                string path1 = location1 + "\\Machines.txt";
                StreamReader sr2 = new StreamReader(path1, Encoding.Default);
                string str1 = sr2.ReadToEnd();
                sr2.Close();
                if(str1 =="")
                {

                    //用户存储文件里
                    string location = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase.ToString());
                    string path = location + "\\Machines.txt";

                    FileStream stream = File.Open(path, FileMode.OpenOrCreate, FileAccess.Write);
                    stream.Seek(0, SeekOrigin.Begin);
                    stream.SetLength(0);
                    stream.Close();

                    StreamWriter sr = new StreamWriter(path, true);
                    sr.Write(username);
                    sr.Flush();
                    sr.Close();
                    //结束
                }
                Program.user = username;
                Program.userid = userid;
                Main main = new Main(Program.userid);
                main.Show();
                this.Hide();
            }
            else 
            {
                MessageBox.Show("登录密码或用户名不正确！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);                  
            }


        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
            //用户存储文件里
            string location = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase.ToString());
            string path = location + "\\Machines.txt";

            FileStream stream = File.Open(path, FileMode.OpenOrCreate, FileAccess.Write);
            stream.Seek(0, SeekOrigin.Begin);
            stream.SetLength(0);
            stream.Close();
        }
    }
}